package com.alan.mr.weibo;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;

/**
 *  第一个map, 计算 TF 和 统计N
 *  TF:当前关键字在该条微博内容中出现的次数
 *  N:微博的总条数。
 * Created by Alan on 2017/10/2.
 */
public class FirstMapper extends Mapper<LongWritable,Text,Text,IntWritable> {

    /**
     * TF 在一个文章中出现的词频 N 总共多少文章
     * 按行传入
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] values = value.toString().trim().split("\t");
        if (values.length >= 2) {
            String id = values[0].trim();
            String content = values[1].trim();
            // 分词
            StringReader stringReader = new StringReader(content);
            IKSegmenter ikSegmenter = new IKSegmenter(stringReader, true);
            Lexeme word = null;
            while ((word = ikSegmenter.next()) != null ) {
                String w = word.getLexemeText();
                //TF:当前关键字在该条微博内容中出现的次数
                context.write(new Text(w + "_" + id), new IntWritable(1));
            }
            // 微博的总条数
            context.write(new Text("count"), new IntWritable(1));
        }else {
            System.out.println(values.toString() + "---");
        }
    }
}
